<template>
  <div class="network-operating-record">
    <search-table
      :columns="columns"
      :data="operatingRecordList"
      @change-page="changePageNow"
      @change-size="changePageSize"
      :total="total"
      :page.sync="pageIndex"
      :size.sync="pageSize"
    >
      <template slot="select-group">
        <Input
          v-model.trim="targetName"
          clearable
          placeholder="目标名称"
          style="width: 180px"
        />
        <Input
          v-model.trim="vulName"
          clearable
          placeholder="漏洞名称"
          style="width: 180px"
        />
        <Input
          v-model.trim="targetUrl"
          clearable
          placeholder="地址"
          style="width: 180px"
        />
        <!-- <Input
          v-model.trim="vulType"
          clearable
          placeholder="漏洞类型"
          style="width: 180px"
        /> -->
        <Select filterable
          clearable
          v-model="vulType"
          placeholder="漏洞类型"
          style="margin-left: 15px; width: 150px"
        >
          <Option
            v-for="(item, key) in assetVulnerabilityTypeOptions"
            :key="key"
            :value="key"
            >{{ item }}</Option
          >
        </Select>
        <Select filterable
          clearable
          v-model="level"
          placeholder="漏洞评级"
          style="width: 150px"
        >
          <Option
            v-for="level in levelList"
            :key="level.value"
            :value="level.value"
            >{{ level.label }}</Option
          >
        </Select>
        <Select filterable
          clearable
          v-model="status"
          placeholder="漏洞状态"
          style="width: 150px"
        >
          <Option
            v-for="level in statusList"
            :key="level.value"
            :value="level.value"
            >{{ level.label }}</Option
          >
        </Select>
        <!-- <DatePicker
          type="datetime"
          v-model.trim="startTime"
          placeholder="提交时间"
          format="yyyy-MM-dd HH:mm:ss"
          style="width: 180px"
          transfer
          @on-change="changeStartTime"
        /> -->
        <DatePicker
          type="datetimerange"
          v-model="submitTime"
          format="yyyy-MM-dd HH:mm:ss"
          placement="bottom-start"
          confirm
          ref="datePicker"
          placeholder="提交时间"
          style="width: 240px"
          transfer
          @on-change="changeDates"
        ></DatePicker>
        <Button
          class="header-btn"
          type="primary"
          ghost
          style="height: 32px"
          @click="changePage(1)"
        >
          查询
        </Button>
        <Button
          class="header-btn"
          type="primary"
          ghost
          style="height: 32px"
          @click="refreshVulnerability()"
        >
          <Icon size="16" type="ios-refresh" />
        </Button>
      </template>
    </search-table>
  </div>
</template>

<script>
import SearchTable from "@/components/SearchTable/SearchTable";
import { getaccessgrouplistdis } from '@/api/access_new';
import { getVul4ContestPage } from '@/api/product';
import { pushVulnerability } from '@/api/referee.js';
import { TimeFormatter } from '@/libs/utils';
export default {
  name: "assetStatistics",
  components: {
    SearchTable
  },
  props: {
    disabledAllBtn: {
      type: Boolean,
      default: false,
    },
    useApiType:{
      type:Number,
      default:()=> null
    },
    sceneId:{
      type:String,
      default:()=> ''
    },
    projectId:{
      type:String,
      default:()=> ''
    },
  },
  data() {
    return {
      assetVulnerabilityTypeOptions: {
          backdoor: '后门',
          'bak-file-found': '发现备份文件',
          'buffer-overflow': '缓冲区溢出',
          'cmd-exec': '命令执行',
          'code-exec': '代码执行',
          'dir-listing': '目录遍历',
          'dir-traversal': '目录穿越',
          dos: '拒绝服务攻击',
          'file-creation': '文件创建',
          'file-deletion': '文件删除',
          'file-download': '文件下载',
          'file-read': '文件读取',
          'file-upload': '文件上传',
          finger: '信息搜集',
          'improper-input-validation': '不当的输入验证',
          'info-disclosure': '信息泄露',
          'ldap-inj': 'LDAP注入',
          lfi: '本地文件包含',
          'login-bypass': '登录绕过',
          'path-disclosure': '路径泄露',
          'privilege-escalation': '提权攻击',
          redirect: '重定向',
          'remote-pass-change': '远程密码修改',
          'resolve-error': '解析错误',
          rfi: '远程文件包含',
          'sec-bypass': '安全绕过',
          'sql-inj': 'SQL注入',
          'ssi-inj': '服务器端包含注入',
          ssrf: '服务器端请求伪造',
          'unauth-access': '未授权访问',
          'xml-inj': 'XML注入',
          'xpath-inj': 'XPath注入',
          xss: '跨站脚本攻击',
          injection: '注入攻击',
          xxe: 'XML外部实体注入',
          'file_upload': '文件上传攻击',
          'file_operation': '文件操作攻击',
          'file_traversal': '文件遍历攻击',
          'file_download': '文件下载攻击',
          rce: '远程代码执行',
          'info_leak': '信息泄露攻击',
          misconfiguration: '配置错误',
          weakpass: '弱口令攻击',
          other: '其它攻击',
      },
      teamName:"",
      submitTime:[],
      operatingRecordList: [],
      total: 0, // 总数据量
      pageSize: 10, // 分页大小
      pageIndex: 1, // 当前页
      targetName:'', // 目标名称
      vulName:'', // 漏洞名称
      status:'', // 状态
      level:'', // 漏洞评级
      targetUrl:'', // url 地址
      vulType:'', // 漏洞类型
      startTime:'', // 提交时间
      endTime:'', // 提交时间
      columns: [
        {
          placement: "top-left",
          title: "目标",
          key: "targetName",
          // align: "center",
          tooltip: true,
          // minWidth: 150,
        },
        {
          title: "漏洞名称",
          width: 150,
          key: "name",
          tooltip: true,
        },
        {
          title: "漏洞评级",
          width: 150,
          key: "level",
          tooltip: true,
          render: (h, params) => {
            let obj = this.mixinLevel(params.row.level)
            return h('Tag', {
              props: {
                color: obj.color
              },
              style: {
                width: '70px',
                'text-align': 'center'
              }
            }, obj.text)
          }
        },
        {
          title: "漏洞评分",
          width: 100,
          key: "value",
          tooltip: true,
        },
        {
          title: '漏洞类型',
          key: 'type',
          tooltip: true,
          render: (h, params) => {
            if (params.row.type) {
              let content = this.assetVulnerabilityTypeOptions[params.row.type]
              return h('Tooltip', {
                  props: {
                      'max-width': '300',
                      transfer: true,
                      content
                  },
                  style: {
                      width: '100%',
                  }
              }, [
                  h('p', {
                      style: {
                          overflow: 'hidden',
                          'text-overflow': 'ellipsis'
                      }
                  }, content)
              ])
            } else {
              return h('span', '无')
            }
          }
        },
        {
          title: '地址',
          key: 'url',
          tooltip: true,
        },
        {
          title: "状态",
          width: 100,
          key: "status",
          tooltip: true,
          render: (h, params) => {
            let color = '';
            let text = '';
            let status = params.row.status ? params.row.status : 1;
            // if (status == 3) {
            //   color = 'success';
            //   text = '已修复';
            // } else if (status == 2) {
            //   color = 'success';
            //   text = '已复测';
            // } else if (status == 1) {
            //   color = 'error';
            //   text = '发现';
            // } else if (status == 4) {
            //   color = 'default';
            //   text = '确认修复';
            // }
            return h('span', [
              h(
                'Tag',
                {
                  props: {
                    color: this.filterStatus(status).color
                  }
                },
                this.filterStatus(status).text
              )
            ]);
          }
        },
        {
          title: "复现方式",
          minWidth: 150,
          key: "step",
          show: this.sceneId ? true : false,
          tooltip: true,
        },
        {
          title: "修复建议",
          minWidth: 150,
          key: "sulotion",
          show: this.sceneId ? true : false,
          tooltip: true,
        },
        {
          title: "描述",
          minWidth: 150,
          key: "description",
          tooltip: true,
        },
        {
          title: "提交时间",
          minWidth: 150,
          key: "created",
          tooltip: true,
        },
        {
            title:'推送状态',
            key:'pushStatus',
            tooltip:true,
            show:()=> this.showPushAction,
            filter:(pushStatus) => pushStatus ? '已推送' : '未推送',
            setColor:(pushStatus) => pushStatus ? 'var(--status-yes)' : 'var(--status-no)',
        },
        {
            title:'推送时间',
            key:'pushTime',
            tooltip:true,
            show:()=> this.showPushAction,
        },
        {
            title:'反馈状态',
            key:'feedbackStatus',
            tooltip:true,
            show:()=> this.showPushAction,
            filter:(feedbackStatus) => feedbackStatus ? '已反馈' : '未反馈',
            setColor:(feedbackStatus) => feedbackStatus ? 'var(--status-yes)' : 'var(--status-no)',
        },
        // {
        //     title:'反馈时间',
        //     key:'feedbackTime',
        //     tooltip:true
        // },
        {
          title: "操作",
          key: "result",
          show:false,
          render: (h, params) => {
            let result = params.row.result;
            return h(
              "Tag",
              {
                props: {
                  color:
                    result ? "blue": 'red',
                },
              },
              result?'成功':'失败'
            );
          }
        },
        {
          title: "操作",
          key: "action",
          show:()=> this.showPushAction,
          opList:[
            {
              txt:"推送",
              disabled:(data) => data.pushStatus == 1,
              fun:(data) => {
                this.pushVulnerability(data.id)
              }
            }
          ]
        },
      ],
      dataList:[],
      levelList:[
        // {value:0,label:'超危'},
        {value:'0',label:'高危'},
        {value:'1',label:'中危'},
        {value:'2',label:'低危'},
        // {value:'3',label:'低危'},
      ],
      statusList:[
        {value:'1',label:'未修复'},
        {value:'2',label:'已复测'},
        {value:'3',label:'修复中'},
        {value:'4',label:'复测中'},
        {value:'5',label:'已修复'}
      ],
      showPushAction:false,
    };
  },
  mounted() {
    this.getqueryLogsList()
    // if(this.useApiType){
    // }
    // if(this.useApiType ===2){
    //   this.columns.splice(1, 0, {
    //     key: 'vendorName',
    //     title: '供应厂商名称',
    //     width: 200,
    //     tooltip: true,
    //   })
    // }
    // this.getQueryTeamList()
    this.$store.dispatch('common/getSysConfigShow','showPushAction').then(data => {
      this.showPushAction = data
    })
  },
  computed: {},
  methods: {
    changeDates(val){
      this.submitTime = val
      if(this.submitTime && this.submitTime.length > 0){
        this.startTime = this.submitTime[0]
        this.endTime = this.submitTime[1]
      }else{
        this.startTime = ''
        this.endTime = ''
      }
    },
    changeStartTime(val){
      this.startTime = val
    },
    // 危险等级
    mixinLevel(val) {
      switch(val) {
        case '1': return { text: '高危', color: '#ed4014' }
        case '2': return { text: '中危', color: '#ff9900' }
        case '3': return { text: '低危', color: '#2db7f5' }
        default: return { text: '未定级', color: '' }
      }
      // switch(val) {
      //   case '0': return { text: '高危', color: '#ed4014' }
      //   case '1': return { text: '中危', color: '#ff9900' }
      //   case '2' || '3': return { text: '低危', color: '#2db7f5' }
      //   default: return { text: '未定级', color: '' }
      // }
    },
    changePage(val) {
      this.pageIndex = val
      this.getqueryLogsList()
    },
    getQueryTeamList(){
      let sceneId = this.$route.query.id || this.sceneId
      getaccessgrouplistdis({
        pageIndex: 1,
        pageSize: 9999,
        isSuperGroup: false
      },sceneId).then(res=>{
        const filter = res.data.items.filter(item => item.teamName !== '目标方管理员组')
        this.dataList = filter
      })
    },
    //查询
    getqueryLogsList(){
      // let api= this.useApiType === 1 ? businessSystem4ContestPage:product4ContestPage
      let sceneId = this.$route.query.id || this.sceneId
      let params = {  
          pageIndex:this.pageIndex,
          pageSize:this.pageSize,
          // teamName:this.teamName,
          contestId: sceneId
        }
        if(this.targetName){
          params.targetName = this.targetName
        }
        if(this.vulName){
          params.vulName = this.vulName
        }
        if(this.level){
          params.level = this.level
        }
        if(this.status){
          params.status = this.status
        }
        if(this.targetUrl){
          params.url = this.targetUrl
        }
        if(this.vulType){
          params.vulType = this.vulType
        }
        if(this.startTime){
          params.startTime = this.startTime
        }
        if(this.endTime){
          params.endTime = this.endTime
        }
        getVul4ContestPage(params).then(data=>{
            if(data.code==1){
                this.operatingRecordList = data.data.items
                this.total = data.data.total
            }
        }).catch(()=>{
            
        })
    },
    // 刷新
    refreshVulnerability() {
      this.pageIndex = 1,
      this.pageSize = 10
      this.targetName = ''
      this.vulName = ''
      this.status = ''
      this.level = ''
      this.targetUrl = ''
      this.vulType = ''
      this.submitTime = []
      this.startTime = ''
      this.endTime = ''
      this.getqueryLogsList()
    },
    // 修改分页大小
    changePageSize(val) {
      this.pageSize = val;
      this.getqueryLogsList();
    },
    // 修改当前页
    changePageNow(val) {
      this.pageIndex = val;
      this.getqueryLogsList();
    },
    // 推送
    pushVulnerability(id){
        pushVulnerability(id,{pushStatus:1,pushTime:TimeFormatter.time(new Date().getTime())}).then(({data:{code,data,message}}) => {
            if(code === 1){
                this.getqueryLogsList();
            }else{
                this.$Message.error(message)
            }
        }).catch(() => {
            this.$Message.error('推送失败')
        })
    },
    filterStatus(status = 1){
        let color = '';
        let text = '';
        if (status == 3) {
            color = 'var(--status-yes)';
            text = '修复中';
        } else if (status == 2) {
            color = 'var(--status-yes)';
            text = '已复测';
        } else if (status == 1) {
            color = 'var(--status-no)';
            text = '未修复';
        } else if (status == 4) {
            color = 'var(--status-pre)';
            text = '复测中';
        } else if (status == 5) {
            color = 'var(--status-pre)';
            text = '已修复';
        }
        return {
            color,
            text
        }
    }
  },
  watch: {},
  beforeDestroy() {},
};
</script>

<style>
.network-operating-record .ivu-table-tip {
  overflow: hidden !important;
}
</style>